<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<style>
	body,dl,dt,dd,p{margin:0;padding:0;}
	body{font-family:Tahoma;font-size:12px;}
	label,input,a{vertical-align:middle;}
	label{padding:0 10px 0 5px;}
	a{color:#09f;text-decoration:none;}
	a:hover{color:red;}
	dl{width:120px;margin:10px auto;padding:10px 5px;border:1px solid #666;border-radius:5px;background:#fafafa;}
	dt{padding-bottom:10px;border-bottom:1px solid #666;}
	dt label{font-weight:700;}
	p{margin-top:10px;}
</style>
</head>
<body>
    	<dl>
    	    <dt><input type="checkbox" id="checkAll" /><label>全选</label><a href="javascript:;" style="float:right">反选</a></dt>
    	    <dd>
    	        <p><input type="checkbox" name="item" id="1" /><label>选项（一）</label></p>
    	        <p><input type="checkbox" name="item" /><label>选项（二）</label></p>
    	        <p><input type="checkbox" name="item" /><label>选项（三）</label></p>
    	        <p><input type="checkbox" name="item" /><label>选项（四）</label></p>
    	        <p><input type="checkbox" name="item" /><label>选项（五）</label></p>
    	        <p><input type="checkbox" name="item" /><label>选项（六）</label></p>
    	        <p><input type="checkbox" name="item" /><label>选项（七）</label></p>
    	        <p><input type="checkbox" name="item" /><label>选项（八）</label></p>
    	        <p><input type="checkbox" name="item" /><label>选项（九）</label></p>
    	        <p><input type="checkbox" name="item" /><label>选项（十）</label></p>
    	    </dd>    
    	</dl>
    	<script>
    		var checkbox=document.getElementsByTagName("input");//获取所有input框的集合
    		var olink=document.getElementsByTagName("a")[0];//获取反选的按钮
    		var label=document.getElementsByTagName("label")[0];//获取全选的文字
    		
    		checkbox[0].onclick=function(){//第一个全选checkbox
    			for (var i = 1; i < checkbox.length; i++) {
    				checkbox[i].checked=this.checked;    				
    			}
    			checkall();
    		}
    		olink.onclick=function(){//反选按钮点击
    			for (var i = 1; i < checkbox.length; i++) {
    				checkbox[i].checked=!checkbox[i].checked;    				
    			}
                checkall();
    		}
    		for (var i = 1; i < checkbox.length; i++) {//遍历每一个checkbox，每次去更新选择框的 状态
    			 checkbox[i].onclick=function(){
    			 	checkall();
    			 }
    		}
    		var checkall=function(){//更新选择的状态
    			for (var i = 1,n=0; i < checkbox.length; i++) {
    				if(checkbox[i].checked) n++;
    			}
    			checkbox[0].checked = n==checkbox.length-1;
                label.innerHTML=  checkbox[0].checked ? "全部不选" : "全选" ;
    		}

    	</script>
</body>
</html>